package cn.kbkwwk.os.application;

import cn.kbkwwk.os.global.GlobalData;
import cn.kbkwwk.os.thread.MyThreadCase4;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.image.Image;
import javafx.stage.Stage;

import java.util.Optional;

/**
 * ClassName: Main
 * Package: cn.kbkwwk.os.application
 * Description:
 *
 * @Date: 2022-06-03 14:16 星期五
 * @Author: Kevin
 */
public class Main extends Application {


    @Override
    public void start(Stage primaryStage) throws Exception {
        GlobalData.mainStage = primaryStage;
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/mainView.fxml"));
            Parent root = (Parent)loader.load();
            GlobalData.myController =  loader.getController();//获取加载的fxml对应的controller对象
            Scene mainScene = new Scene(root);
            GlobalData.mainStage.setScene(mainScene);
            GlobalData.mainStage.setResizable(false);
            GlobalData.mainStage.getIcons().add(new Image("/img/logo.png"));
            GlobalData.mainStage.setTitle("哲学家问题");//窗口标题
            GlobalData.myController.getCase4().setSelected(true);//初始化默认选择case4
            GlobalData.mainStage.show();
            GlobalData.mainStage.setOnCloseRequest(event -> {
                Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
                alert.setTitle("关闭");
                alert.setHeaderText("是否确认关闭程序?");
                Optional<ButtonType> result = alert.showAndWait();
                if(result.get() == ButtonType.OK){
                    MyThreadCase4.setIsLoop(false);//设置所有线程关闭
                    System.out.println("Stage is closing");
                    System.exit(0);//正常结束进程
                }else{
                    event.consume();
                    System.out.println("Stage isn't closing");
                }
            });
        } catch(Exception e) {
            e.printStackTrace();
        }
    }


//    启动类
    public static void main(String[] args) {
        launch(args);
    }


}
